// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ICache from "../cache/ICache";
import { MONITOR_LIST, selfCls } from "../common/Decorator";
import MsgMgr from "../event/MsgMgr";
import CCMP from "./CCMP";


const {ccclass, property} = cc._decorator;

@selfCls
export default class GameObject extends CCMP {
    protected onLoad(): void {
        this._data = this.initCache();
        super.onLoad();
    }
    protected initMonitor() {
        let list:[] = this[MONITOR_LIST] || [];
        for (let i = 0; i < list.length; i++) {
            list[i] && (list[i] as Function)(this, this._data);
        }
    }

    protected closeMonitor() {
        let list:[] = this[MONITOR_LIST] || [];
        for (let i = 0; i < list.length; i++) {
            list[i] && (list[i] as Function)(this, this._data, true);
        }
    }
    public async init(args?) {
        super.init(args);
        
        await this.initSection();
        this.isRunning = true;
    }
    async initSection() {

    }

    initCache():ICache<any> {
        return null;
    }

    private _data:ICache<any> = null;
    public cache<T extends ICache<any>>():T {
        return this._data as T;
    }

    addCCMP<T extends CCMP, E>(type: {new(): T;}, args?:E) {
        this.addComponent(type).init(args);
    }

}
